package com.gccloud.design;


import com.gccloud.design.dto.BwpActivityRepoDto;
import com.gccloud.design.dto.PageObj;
import com.gccloud.design.service.LinkComponentsService;
import com.gccloud.design.vo.OptButtonBranchVo;
import com.gccloud.starter.common.vo.R;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 实现功能：环节组件
 *
 * @author xiao.ao
 */
@Slf4j
@Api(tags = "环节组件")
@RequestMapping("/linkComponents")
@RestController
public class LinkComponentsController {


    @Resource
    private LinkComponentsService linkComponentsService;

    /**
     * 查询树数据，获取业务类型
     *
     * @return List
     */
    @ApiOperation(value = "查询树数据，获取业务类型")
    @RequestMapping(value = "/getBusinessTree", method = RequestMethod.POST)
    public R<List<Map<String, Object>>> getBusinessTree() {
        List<Map<String, Object>> parentList = linkComponentsService.getDataByParentId();
        return R.success(parentList);
    }

    /**
     * 查询环节组件
     *
     * @return Map
     */
    @GetMapping("/getDataList")
    @ApiOperation(value = "列表", notes = "查询环节组件", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<Map<String, Object>> getDataList(BwpActivityRepoDto bwpActivityRepoDto) {
        return R.success(linkComponentsService.getDataList(bwpActivityRepoDto));
    }

    /**
     * 查询环节绑定操作
     *
     * @param bwpActivityRepoDto 环节库实体
     * @return list
     */
    @PostMapping(value = "/getBind")
    @ApiOperation(value = "查询环节绑定操作")
    public R<List<OptButtonBranchVo>> getBind(@RequestBody BwpActivityRepoDto bwpActivityRepoDto) {
        log.info("查询环节组件,参数为：repoId:{}", bwpActivityRepoDto.getRepoId());
        return R.success(linkComponentsService.getBind(bwpActivityRepoDto.getRepoId()));
    }

    /**
     * 获取执行逻辑和补偿逻辑
     *
     * @param type 类型
     * @return map
     */
    @GetMapping(value = "/getMethod")
    @ApiOperation(value = "获取执行逻辑和补偿逻辑")
    public R<Map<String, Object>> getMethod(String type) {
        log.info("获取执行逻辑和补偿逻辑,参数为：type:{}", type);
        return R.success(linkComponentsService.getMethod(type));
    }

    @ApiOperation(value = "绑定弹窗查询")
    @RequestMapping(value = "/getBindForm", method = RequestMethod.GET)
    public R<Map<String, Object>> getBindForm(String repoId, String optBranchCode, String optBranchName, PageObj pageObj) {
        log.info("绑定弹窗查询,参数为：optBranchCode:{},optBranchName:{}", optBranchCode, optBranchName);
        return R.success(linkComponentsService.getBindForm(repoId, optBranchCode, optBranchName, pageObj));
    }

    @GetMapping(value = "/getForm")
    @ApiOperation(value = "获取页面模板")
    public R<Map<String, Object>> getForm(Integer businessTypeId, String repoId) {
        log.info("获取页面模板,参数为：businessTypeId:{},repoId:{}", businessTypeId, repoId);
        return R.success(linkComponentsService.getForm(businessTypeId, repoId));
    }

    @ApiOperation(value = "新增/修改 保存操作")
    @RequestMapping(value = "/saveActivityRepo", method = RequestMethod.POST)
    public R<Object> saveActivityRepo(@RequestBody BwpActivityRepoDto bwpActivityRepoDto) {
        linkComponentsService.saveActivityRepo(bwpActivityRepoDto);
        return R.success();
    }

    @PostMapping(value = "/batchDelete")
    @ApiOperation(value = "批量删除环节组件")
    public R<Boolean> batchDelete(@RequestBody BwpActivityRepoDto bwpActivityRepoDto) {
        log.info("开始批量删除环节组件,参数为：ids:{}", bwpActivityRepoDto.getIds());
        linkComponentsService.batchDelete(bwpActivityRepoDto.getIds());
        return R.success();
    }

    @PostMapping(value = "/selectForm")
    @ApiOperation(value = "绑定模板")
    public R<Boolean> selectForm(@RequestBody BwpActivityRepoDto bwpActivityRepoDto) {
        log.info("开始绑定模板,参数为：repoId:{},id:{}", bwpActivityRepoDto.getRepoId(), bwpActivityRepoDto.getId());
        linkComponentsService.selectForm(bwpActivityRepoDto.getRepoId(), bwpActivityRepoDto.getId());
        return R.success();
    }

    @PostMapping(value = "/noBindForm")
    @ApiOperation(value = "取消绑定模板")
    public R<Object> noBindForm(@RequestBody BwpActivityRepoDto bwpActivityRepoDto) {
        log.info("开始取消绑定模板,参数为：repoId:{},id:{}", bwpActivityRepoDto.getRepoId(), bwpActivityRepoDto.getId());
        return linkComponentsService.noBindForm(bwpActivityRepoDto.getRepoId(), bwpActivityRepoDto.getId());
    }

    @PostMapping(value = "/isBind")
    @ApiOperation(value = "绑定环节操作")
    public R<Boolean> isBind(@RequestBody BwpActivityRepoDto bwpActivityRepoDto) {
        log.info("开始绑定操作,参数为：repoId:{},ids:{}", bwpActivityRepoDto.getRepoId(), bwpActivityRepoDto.getIds());
        linkComponentsService.updateActivity(bwpActivityRepoDto.getRepoId(), bwpActivityRepoDto.getIds());
        return R.success();
    }

    @PostMapping(value = "/noBind")
    @ApiOperation(value = "绑定")
    public R<Boolean> noBind(@RequestBody BwpActivityRepoDto bwpActivityRepoDto) {
        log.info("开始取消绑定操作,参数为：repoId:{},ids:{}", bwpActivityRepoDto.getRepoId(), bwpActivityRepoDto.getIds());
        linkComponentsService.noBindUpdate(bwpActivityRepoDto.getRepoId(), bwpActivityRepoDto.getIds());
        return R.success();
    }
}
